local mainscreen = require "screens/mainscreen"
local Image = require "widgets/image"
local Text 	= require "widgets/text"
local Widget = require "widgets/widget"
local TextButton = require "widgets/textbutton"
local PopupDialogScreen = require "screens/popupdialog"
local STRINGS = STRINGS
local Vector3 = Vector3

TEST_LEVEL = "rg_foresttrail"
-- TEST_LEVEL = "rg_paraworld"

table.insert(Assets, Asset("ATLAS", "images/bg_loading_loading_newhorizons.xml"))

local function DisableModAndRestart()
	RG_MOD_ACTIVATED = false --@--

	local function DoDisable()
		KnownModIndex:Disable(env.modname)
		KnownModIndex:Save()
		SimReset()
	end

	TheFrontEnd:PushScreen(PopupDialogScreen(STRINGS.UI.MAINSCREEN.RG_QUIT_DIALOG_TITLE, STRINGS.UI.MAINSCREEN.RG_QUIT_DIALOG_TEXT,
	{
		{text=STRINGS.UI.MODSSCREEN.RESTART, cb = DoDisable},
		{text=STRINGS.UI.MODSSCREEN.CANCEL,  cb = function() TheFrontEnd:PopScreen() end}
	}))
end

AddClassPostConstruct("screens/mainscreen", function(self)
	-- DLC check
	local dlc = {}
	local num = 3
	local scale = 0.5
	for i = 1, num do
		dlc[i] = IsDLCInstalled(i)
		print("DLC "..i.." is installed: ", dlc[i])
	end


	local function OnGainFocus(self)
		self:ScaleTo(scale, scale*1.1, 0.1)
		self.colour[4] = 1
		self:SetTint(unpack(self.colour))
		local line = STRINGS.UI.MAINSCREEN["RG_DLC_".. (self.on and "ON" or "OFF")]
		local dlc = STRINGS.UI.MAINSCREEN.RG_DLC_NAMES[self.dlcname:upper()]
		self.hint:SetString(string.format(line, dlc))
	end

	local function OnLoseFocus(self)
		self:ScaleTo(scale, scale, 0)
		self.colour[4] = 0.5
		self:SetTint(unpack(self.colour))
	end

	local function AddDLCChecker()
		local root = self.fixed_root:AddChild(Widget("dlc-check-root"))
		root:SetVAnchor(ANCHOR_TOP)
		root:SetHAnchor(ANCHOR_LEFT)
		root:SetPosition(100, -100)
		root.dlc = dlc

		local text = root:AddChild(Text(UIFONT, 32, ""))
		text:SetPosition(140, -50)

		for i,v in ipairs{"rog", "sw", "ham"} do
			local postfix = dlc[i] and "_on.tex" or "_off.tex"
			local colour = dlc[i] and {.5, 1, 0.5, 0.5} or {1, .5, .5, 0.5}
			local img = root:AddChild(Image("images/ui.xml", v..postfix))
			img.on = dlc[i]
			img.dlcname = v
			img.colour = colour
			img:SetScale(scale)
			img:SetTint(unpack(colour))
			img:SetPosition(i*70+0, 0)
			img.OnGainFocus = OnGainFocus
			img.OnLoseFocus = OnLoseFocus
			img.hint = text
		end
	end

--[[ ON ]] if RG_MOD_ACTIVATED then

	AddDLCChecker()

	-- title
	self.updatename:SetString("Don't Starve - Rougelike mode beta")

	for _,v in pairs{
		self.motd,
		self.wilson,
		self.shield,
		self.banner,
		self.submenu,
		self.promo,
		self.screecher,
		self.beta_reg,
		self.RoGUpgrade,
		self.chester_upsell,
	}do
		if v ~= nil then 
			v:Kill()
		end
	end

	self.bg:SetTexture("images/bg_loading_loading_newhorizons.xml", "loading_newhorizons.tex")
	self.bg:SetTint(1,1,1,1)

	-- self.logo = self.fixed_root:AddChild(Image("images/screecher_logo.xml", "screecher_logo.tex"))
	-- self.logo = self.fixed_root:AddChild(Image())
 --    self.logo:SetVRegPoint(ANCHOR_MIDDLE)
 --    self.logo:SetHRegPoint(ANCHOR_MIDDLE)
 --    self.logo:SetPosition(400-70, 210-50, 0)
 --    self.logo:SetScale(0.5)

	-- this prevents stale component errors
	self.SetMOTD = function() return end

	local function onsaved()
		StartNextInstance({reset_action=RESET_ACTION.LOAD_SLOT, save_slot = "rg_save_slot"})
	end

	local starting = false

	local function StartGame()
		if starting then 
			return
		else
			starting = true
		end

		EnableAllDLC() --@--
		DisableAllDLC()
		TheFrontEnd:GetSound():KillSound("FEMusic")
		TheFrontEnd:Fade(false, 0.1, function()
			SaveGameIndex:StartSurvivalMode("rg_save_slot", "wilson", {
				level_id = RG_LEVELS_INDEX[TEST_LEVEL:upper() --[["RG_START"]]],
				tweak = {
					misc = {
						location = TEST_LEVEL,--"rg_start",
					}
				},
			}, onsaved, nil, "custom")
		end)
	end

	local function CheckDLCAndStart()
		local canstart = dlc[1] and dlc[2]
		local showwarning = not canstart or not dlc[3]
		if not showwarning then
			StartGame()
		else
			local buttons = {
				{text=STRINGS.UI.MAINSCREEN.RG_DLC_CANCEL,  cb = function() TheFrontEnd:PopScreen() end}
			}
			if canstart then
				table.insert(buttons, 1, 
					{text=STRINGS.UI.MAINSCREEN.RG_DLC_OK, cb = StartGame})
			end
			TheFrontEnd:PushScreen(PopupDialogScreen(STRINGS.UI.MAINSCREEN.RG_DLC_REQUIEMENT_TITLE, string.format(STRINGS.UI.MAINSCREEN.RG_DLC_REQUIEMENT, modname), buttons))
		end
	end

	--self.menu.horizontal = true
	self.menu.offset = 70
	self.MainMenu = function(self)		
		local menu_items = {}
		table.insert( menu_items, {text=STRINGS.UI.MAINSCREEN.RG_START, cb=CheckDLCAndStart, offset = Vector3(0,0,0)})
		table.insert( menu_items, {text=STRINGS.UI.MAINSCREEN.RG_SETTINGS, cb= function() self:DoOptionsMenu() end})
		table.insert( menu_items, {text=STRINGS.UI.MAINSCREEN.RG_DONTSTARVE, cb= DisableModAndRestart })
		table.insert( menu_items, {text=STRINGS.UI.MAINSCREEN.EXIT, cb= function() self:OnExitButton() end})
		self:ShowMenu(menu_items)
		self.main_menu = true
	end

	self.DoOptionsMenu = function(self)
		local menu_items = {}
		if PLATFORM == "NACL" then
			table.insert( menu_items, {text=STRINGS.UI.MAINSCREEN.ACCOUNTINFO, cb= function() self:ProductKeys() end})
			if IsGamePurchased() then
				table.insert( menu_items, {text=STRINGS.UI.MAINSCREEN.ENTERKEY, cb= function() self:EnterKey() end})
			end
		end
		table.insert( menu_items, {text=STRINGS.UI.MAINSCREEN.SETTINGS, cb= function() self:Settings() end})
		table.insert(menu_items, {text=STRINGS.UI.MAINSCREEN.CONTROLS, cb= function() self:OnControlsButton() end})
		
	    if not IsRail() then
	   	   table.insert(menu_items, {text=STRINGS.UI.MAINSCREEN.LANGUAGES, cb= function() self:OnLanguageButton() end})
	    end

		table.insert( menu_items, {text=STRINGS.UI.MAINSCREEN.CANCEL, cb= function() self:MainMenu() end})

		self:ShowMenu(menu_items)
	end

	self.OnUpdate = function(self, dt)
		
	end

	local old_show = self.ShowMenu
	function self:ShowMenu(menu_items, x, y, ...)
		if dlc[3] then
			if y then
				y = y - 100
			end
			if x then
				x = x - 80
			end
		end
		old_show(self, menu_items, x, y, ...)
	end

	-- self.ShowMenu = function(self, menu_items)
	-- 	self.mainmenu = false
	-- 	self.menu:Clear()
		
	-- 	for k,v in ipairs(menu_items) do
	-- 		self.menu:AddItem(v.text, v.cb, v.offset)
	-- 	end

	-- 	self.menu:SetPosition(
	-- 			-110,--320,--RESOLUTION_X/2,-- -200 + self.menu.offset * (#menu_items-1),
	-- 			-270,-- -RESOLUTION_Y/2,
	-- 			0
	-- 		)
	-- 	self.menu:SetFocus()
	-- end

	-- self.menu.AddItem = function(self, text, cb, offset)
	-- 	local pos = Vector3(0,0,0)
	-- 	pos.y = pos.y + self.offset * #self.items
		
	-- 	if offset then
	-- 		pos = pos + offset	
	-- 	end
		
	-- 	local button = self:AddChild(TextButton())
	-- 	button:SetPosition(pos)
	-- 	button:SetText(text)

	-- 	button:SetTextColour(0.9,0.8,0.6,1)
	-- 	button:SetOnClick( cb )
	-- 	button:SetFont(BUTTONFONT)
	-- 	button:SetTextSize(40)    

	-- 	table.insert(self.items, button)

	-- 	self:DoFocusHookups()
	-- 	return button
	-- end

	self:MainMenu()
	self.menu:SetFocus()
	self.menu:SetPosition(0, -190)

	TheFrontEnd:GetSound():KillSound("FEMusic")
	-- TheFrontEnd:GetSound():PlaySound("scary_mod/music/gamemusic", "FEMusic")

--[[ OFF ]] else
--
end

end)

-- 


AddClassPostConstruct("widgets/containerwidget", function(self)
	local old_Open = self.Open
	function self:Open(container, doer, ...)
		old_Open(self, container, doer, ...)
		if self.container ~= nil and self.container.prefab == "rg_backpack" then
			for _,v in ipairs(self.inv)do
				v.bgimage:SetTint(0.6,0.6,1,1)
			end
		end
	end

	local old_OnUpdate = self.OnUpdate
	function self:OnUpdate(...)
		if self.container ~= nil and self.container.prefab == "rg_backpack" then 
			return
		else
			return old_OnUpdate(self, ...)
		end
	end
end)
